package extract.helper;

import com.fasterxml.jackson.databind.JsonNode;
import common.config.IndexTypeNames;
import common.dao.elastic.ElasticSearchDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Common on 11/26/16.
 */
public class ElasticChangeHelper {

    public static final Logger logger = LoggerFactory.getLogger(ElasticChangeHelper.class);


    private ElasticSearchDao es_dao;
    private IndexTypeNames.ESType es_type;

    private String es_index_name = IndexTypeNames.MediaDB.IndexName;
    private String es_type_name;


    public ElasticChangeHelper(ElasticSearchDao es_dao, IndexTypeNames.ESType es_type){
        this.es_dao  = es_dao;
        this.es_type = es_type;
        this.es_type_name = IndexTypeNames.MediaDB.getTypeNameByType(this.es_type);
    }

    public boolean saveToElastic(JsonNode doc){
        String id = doc.get("_id").asText();
        return this.es_dao.index(this.es_index_name, this.es_type_name,id, doc);
    }

    public boolean deleteFromElastic(String id){
        return this.es_dao.delete(this.es_index_name, this.es_type_name,id);
    }

}
